﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Views;
using Presenters;

namespace SuperQuery
{
    /// <summary>
    /// Interaction logic for SaveItems.xaml
    /// </summary>
    public partial class SaveItems : Window, ISaveItemsView
    {
        #region Public

        /// <summary>
        /// Initialise the SaveItems window
        /// </summary>
        /// <param name="mainWindowPresenter">Reference to the presenter on the main window</param>
        /// <param name="queryViewsToSave">A list of Query Views that contain the data we want to save</param>
        public SaveItems(List<IQueryView> queryViewsToSave)
            : this()
        {
            // Set up datacontext so that data binding will work
            DataContext = this;

            m_Presenter = new SaveItemsPresenter(this, queryViewsToSave);
        }

        /// <summary>
        /// Initialise the SaveItems window
        /// </summary>
        private SaveItems()
        {
            InitializeComponent();
        }

        #region Implementation of IExportResultsView

        /// <summary>
        /// A List of query views that need saving
        /// </summary>
        public List<IQueryView> QueryViewsToSave
        {
            get
            {
                return (List<IQueryView>)GetValue(QueryViewsToSaveProperty);
            }
            set
            {
                SetValue(QueryViewsToSaveProperty, value);
            }
        }

        /// <summary>
        /// A list of query views that the user has selected to save
        /// </summary>
        public List<IQueryView> QueriesSelectedToSave
        {
            get;
            set;
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for QueryViewsToSave.  This enables animation, styling, binding, etc...
        /// </summary>
        private static readonly DependencyProperty QueryViewsToSaveProperty =
            DependencyProperty.Register("QueryViewsToSave", typeof(List<IQueryView>), typeof(SaveItems), new UIPropertyMetadata(null));

        #endregion

        #endregion Public

        #region Private

        /// <summary>
        /// Event raised when the user clicks the cancel button
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            m_Presenter.CancelSave();
            Close();
        }

        /// <summary>
        /// Event raised when the user clicks the "Don't Save" button
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        private void btnNoSave_Click(object sender, RoutedEventArgs e)
        {
            m_Presenter.SaveNone();
            Close();
        }

        /// <summary>
        /// Event raised when the user clicks the "Save" button
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            m_Presenter.SaveItems();
            Close();
        }

        /// <summary>
        /// The presenter for this window
        /// </summary>
        private SaveItemsPresenter m_Presenter;

        #endregion Private
    }
}
